{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "_cell_guid": "762b4321-9818-4397-88fc-f5090d48aefc",
    "_execution_state": "idle",
    "_uuid": "f36ff51f7a8011531c0fc6f45a8e1bca37980870"
   },
   "source": [
    "# 项目练习（1）\n",
    "\n",
    "## <font color = red>满分：100，70分算打卡成功</font>\n",
    "\n",
    "#### 一、简介\n",
    "\n",
    "《权力的游戏》大致是根据小说《冰与火之歌》的故事线情节，故事背景设置于虚构的维斯特洛七王国及厄斯索斯大陆。 该系列记录了该领域贵族争夺铁王座的激烈王朝斗争，而其他家庭则为争取独立而斗争。\n",
    "\n",
    "\n",
    "\n",
    "#### 二、数据集介绍\n",
    "该数据集包含了三个CSV文件，分别是：battles.csv，character-deaths.csv和character-predictions.csv。现分别介绍如下：\n",
    "\n",
    "battles.csv：《权利的游戏》中所有的战争。\n",
    "character-deaths.csv：人物死亡数据集。\n",
    "character-predictions.csv：人物命运预测数据集。\n",
    "在每一个CSV数据里面都包含了大量的变量，读懂这些变量名所代表的含义非常有必要。例如：battles.csv中的year变量代表战争发生的时间，battle_type代表战役类型，有伏击战，围攻战，突袭战等。\n",
    "\n",
    "\n",
    "#### Context\n",
    "\n",
    "Game of Thrones is a hit fantasy tv show based on the equally famous book series \"A Song of Fire and Ice\" by George RR Martin. The show is well known for its vastly complicated political landscape, large number of characters, and its frequent character deaths.\n",
    "\n",
    "#### Content\n",
    "\n",
    "Of course, it goes without saying that this dataset contains spoilers ;)\n",
    "\n",
    "This dataset combines three sources of data, all of which are based on information from the book series.\n",
    "\n",
    "- Firstly, there is battles.csv which contains Chris Albon's \"The\n",
    "War of the Five Kings\" Dataset. Its a\n",
    "great collection of all of the battles in the series.\n",
    "\n",
    "- Secondly we have character-deaths.csv from Erin Pierce and Ben\n",
    "Kahle. This dataset was created as a part of their Bayesian Survival\n",
    "Analysis.\n",
    "\n",
    "- Finally we have a more comprehensive character dataset with\n",
    "character-predictions.csv. It\n",
    "includes their predictions on which character will die.\n",
    "\n",
    "#### Acknowledgements\n",
    "\n",
    "- Firstly, there is battles.csv which contains Chris Albon's \"The war of the Five Kings\" Dataset, which can be found here:\n",
    "\n",
    "https://github.com/chrisalbon/war_of_the_five_kings_dataset . \n",
    "\n",
    "It's a great collection of all of the battles in the series.\n",
    "\n",
    "- Secondly we have character-deaths.csv from Erin Pierce and BenKahle. This dataset was created as a part of their Bayesian Survival\n",
    "Analysis which can be found here: http://allendowney.blogspot.com/2015/03/bayesian-survival-analysis-for-game-of.html\n",
    "\n",
    "- Finally we have a more comprehensive character dataset with character-predictions.csv. This comes from the team at A Song of Ice and Data who scraped it from http://awoiaf.westeros.org/ . It also includes their predictions on which character will die, the methodology of which can be found here: https://got.show/machine-learning-algorithm-predicts-death-game-of-thrones\n",
    "\n",
    "- Inspiration\n",
    "\n",
    "What insights about the complicated political landscape of this fantasy world can you find in this data?\n",
    "  \n",
    "  \n",
    "- https://asoiaf.fandom.com/zh/wiki/%E6%97%8F%E8%AF%AD%E5%88%97%E8%A1%A8?variant=zh-hk"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据准备"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 导入模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_cell_guid": "e4a6ad63-122e-44a9-9a17-50fc75cc9d87",
    "_execution_state": "idle",
    "_uuid": "f7a98aa52426b1353eae4adb2e0cf348438d9eea"
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "from cycler import cycler\n",
    "from collections import Counter\n",
    "import matplotlib.patches as mpatches\n",
    "\"style用bmh\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基础设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_cell_guid": "701b2c84-0c8f-4456-90b1-bdb418228861",
    "_execution_state": "idle",
    "_uuid": "a036de605a0746314e96fe4304a504dd14893f75"
   },
   "outputs": [],
   "source": [
    "plt.rcParams[\"axes.labelsize\"] = 16.\n",
    "plt.rcParams[\"xtick.labelsize\"] = 14.\n",
    "plt.rcParams[\"ytick.labelsize\"] = 14.\n",
    "plt.rcParams[\"legend.fontsize\"] = 12.\n",
    "plt.rcParams[\"figure.figsize\"] = [12., 8.]\n",
    "mpl.rcParams['axes.prop_cycle'] = cycler(color=['r', 'g', 'b', 'y'])\n",
    "\n",
    "\"设置展示最大行数和列数都为8\"\n",
    "\"如何避免画图出现中文乱码？\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_cell_guid": "e36b630b-0d43-46cd-a6ec-266b10ac84c8",
    "_execution_state": "idle",
    "_uuid": "d483ad807be792e5e4eb17c2423333f07c0a98dd"
   },
   "outputs": [],
   "source": [
    "battles = \"导入数据\"\n",
    "character_deaths = \"导入数据\"\n",
    "character_predictions = \"导入数据\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_cell_guid": "2bb21e8b-7d19-438e-8fb9-18bb2457a400",
    "_uuid": "c2bf1a22f55c5ea130698fbcf29450ff891548bb"
   },
   "source": [
    "### 新的特征\n",
    "1. *defender_count* ：防守方的主要家族数量\n",
    "2. *attacker_count* ：进攻方的主要家族数量\n",
    "3. *att_comm_count* ：进攻方的指挥者个数\n",
    "4. *no_of_books* ：一个角色出现在多少本书里"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_cell_guid": "0b3cb149-9773-4ff1-a260-61f6a4681c48",
    "_execution_state": "idle",
    "_uuid": "52f226ef98cc5c4b8698e4108c37eb99dd0e4c2f"
   },
   "outputs": [],
   "source": [
    "battles.loc[:, \"defender_count\"] = \"你的代码\"\n",
    "battles.loc[:, \"attacker_count\"] = \"你的代码\"\n",
    "battles.loc[:, \"att_comm_count\"] = \"你的代码\"\n",
    "character_predictions.loc[:, \"no_of_books\"] = \"你的代码\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_cell_guid": "1cb2efaa-3ca6-4307-ac37-bddd871baa86",
    "_execution_state": "idle",
    "_uuid": "4ff367f70e145450b9624895a77a9ab380fa7fcd"
   },
   "source": [
    "# 数据分析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_cell_guid": "4ffca823-3696-4137-b5f9-58b327ebcb99",
    "_uuid": "91b8e5c9c51a748c06f051fe4ce977941c7f3ac9"
   },
   "source": [
    "<div class=\"alert alert-block alert-success\"><b>Step 1 </b>每年的主要角色被杀或者被捕获总数（8分）\n",
    "\n",
    "\n",
    "- 画出下图\n",
    "\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_cell_guid": "645bd299-550d-45ad-ada8-d69022d312f5",
    "_execution_state": "idle",
    "_uuid": "4aa497cb3db5aaed381bd5f0a9752135b0753bd3"
   },
   "outputs": [],
   "source": [
    "'你的代码'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://pic.imgdb.cn/item/62ea52bd16f2c2beb14ca069.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_cell_guid": "62657591-68d6-4de1-96b5-5d0ed3c9b893",
    "_uuid": "60b440a4391c0672f25dd1e4fbce0951b147ed06"
   },
   "source": [
    "<div class=\"alert alert-block alert-success\"><b>Step 2 </b>军队规模对于胜利的影响（15分）\n",
    "\n",
    "\n",
    "- 根据\"attacker_size\", \"defender_size\", \"attacker_outcome\"分析进攻和防守端的规模对于胜利的影响如何？\n",
    "- 画出下图\n",
    "\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_cell_guid": "5d4ae3d7-d48e-4334-8882-455815264c5f",
    "_execution_state": "idle",
    "_uuid": "cbc2a8c9443b2d6bdcbacaa1b5bf55731f0a262e"
   },
   "outputs": [],
   "source": [
    "'你的代码'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://pic.imgdb.cn/item/62ea52d616f2c2beb14cbd1b.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_cell_guid": "5c821739-805d-4cc5-b622-5d7a00d6a6c4",
    "_uuid": "9349789aebe800f9c41260a29c9378c47622ec2f"
   },
   "source": [
    "<div class=\"alert alert-block alert-success\"><b>Step 3 </b>不同数量的主要进攻方家族参与的战役数（10分）\n",
    "\n",
    "- 根据进攻方的主要家族个数，计算对应的战役次数\n",
    "- 画图。\n",
    "\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_cell_guid": "b9c8e374-9127-4f0c-bbfe-cc20a2865870",
    "_execution_state": "idle",
    "_uuid": "cf5f93a04927e088351d8d44539a6c7cae19a501"
   },
   "outputs": [],
   "source": [
    "'你的代码'"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://pic.imgdb.cn/item/62ea52f016f2c2beb14cd741.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_cell_guid": "11f238c9-20b4-4f0c-a489-d0e938741309",
    "_uuid": "070f4401b24775e8740ad027cd5c18a14250197a"
   },
   "source": [
    "<div class=\"alert alert-block alert-success\"><b>Step 4 </b>最多次发生战斗的对战方（15分）\n",
    "\n",
    "- 按照进攻和防守的交手次数的倒序排列，画出下面的图\n",
    "\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'你的代码'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://pic.imgdb.cn/item/62ea842416f2c2beb186cc1d.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_cell_guid": "5ca4f66c-4d04-448b-bd94-63068b16462f",
    "_uuid": "b188e1692f7f75338690c47fc4174d83694f9a5d"
   },
   "source": [
    "<div class=\"alert alert-block alert-success\"><b>Step 5 </b>不同国王的军队在输和赢的战役中各有多少指挥官（12分）\n",
    "\n",
    "- 画图\n",
    "\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_cell_guid": "0322c93b-f4be-45bf-89dd-f53c161f050b",
    "_execution_state": "idle",
    "_uuid": "ddf1921936876416fd79a65b7b3db3b32234c19d"
   },
   "outputs": [],
   "source": [
    "'你的代码'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://pic.imgdb.cn/item/62ea536e16f2c2beb14d5a06.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_cell_guid": "d5a4fe93-68f4-4082-b90a-bf77fed59c48",
    "_uuid": "f651178cab0795b32871d3cd6fe5b6a8fe05c901"
   },
   "source": [
    "<div class=\"alert alert-block alert-success\"><b>Step 6 </b>每个地区发生了多少次重大死亡/俘虏事件？（12分）\n",
    "\n",
    "- 画图\n",
    "\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'你的代码'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://pic.imgdb.cn/item/62ea53c116f2c2beb14db072.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_cell_guid": "db3321c9-568b-4a8b-89b7-c61885437b21",
    "_uuid": "24ae52072fb7cf06a648a124712e8bd3e227d823"
   },
   "source": [
    "<div class=\"alert alert-block alert-success\"><b>Step 7 </b>存活率和是否和具有死亡关系和出现在第几本书有关？（13分）\n",
    "\n",
    "- 画图\n",
    "\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'你的代码'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://pic.imgdb.cn/item/62ea53fe16f2c2beb14df9b6.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_cell_guid": "d2f49a99-46db-4836-9439-a9350dda0d49",
    "_uuid": "2080ada9a2e3bf03dc073d851e5ee56d1b7d9650"
   },
   "source": [
    "<div class=\"alert alert-block alert-success\"><b>Step 8 </b>军队规模大就一定赢么？（15分）\n",
    "\n",
    "- 计算每个战役的攻守方人数差异，胜利则赋予红色，失败则赋予绿色。\n",
    "\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_cell_guid": "b7261f41-6779-4ecb-8df3-d9f0e3263263",
    "_execution_state": "idle",
    "_uuid": "fcbecb9e8fee36e4985ac93e834e5f75efd245e0"
   },
   "outputs": [],
   "source": [
    "'你的代码'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://pic.imgdb.cn/item/62ea545216f2c2beb14e5e45.jpg)"
   ]
  }
 ],
 "metadata": {
  "_change_revision": 0,
  "_is_fork": false,
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
